﻿Imports System
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Web

Namespace DNNCorp.Web
    Public Class WebFilterStream
        Inherits Stream

#Region "Fields"
        Private m_OutputStream As Stream
        Private m_Position As Long
        Private m_ResponseHTML As StringBuilder
#End Region

#Region "Constructors"
        ''' <summary>
        ''' Creates a new instance of WebFilterStream
        ''' </summary>
        ''' <param name="outputStream"></param>
        Public Sub New(ByVal outputStream As Stream)
            MyBase.New()
            m_OutputStream = outputStream
            ResponseHTML = New StringBuilder
        End Sub
#End Region

#Region "Properties"
        Public Property ResponseHTML() As StringBuilder
            Get
                Return m_ResponseHTML
            End Get
            Set(ByVal Value As StringBuilder)
                m_ResponseHTML = Value
            End Set
        End Property

        Private m_CurrentMatchReplace As String
        Public Property CurrentMatchReplace() As String
            Get
                Return m_CurrentMatchReplace
            End Get
            Set(ByVal Value As String)
                m_CurrentMatchReplace = Value
            End Set
        End Property
        Public Property OutputStream() As Stream
            Get
                Return m_OutputStream
            End Get
            Set(ByVal Value As Stream)
                m_OutputStream = Value
            End Set
        End Property

        Public Overrides ReadOnly Property CanRead() As Boolean
            Get
                Return True
            End Get
        End Property

        Public Overrides ReadOnly Property CanSeek() As Boolean
            Get
                Return True
            End Get
        End Property

        Public Overrides ReadOnly Property CanWrite() As Boolean
            Get
                Return True
            End Get
        End Property
#End Region

#Region "Methods"
        Public Overrides Sub Close()
            OutputStream.Close()
        End Sub

        Public Overrides Sub Flush()
            OutputStream.Flush()
        End Sub

        Public Overrides ReadOnly Property Length() As Long
            Get
                Return OutputStream.Length()
            End Get
        End Property

        Public Overrides Property Position() As Long
            Get
                Return m_Position
            End Get
            Set(ByVal Value As Long)
                m_Position = Value
            End Set
        End Property

        Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
            Return OutputStream.Read(buffer, offset, count)
        End Function

        Public Overrides Function Seek(ByVal offset As Long, ByVal origin As System.IO.SeekOrigin) As Long
            Return OutputStream.Seek(offset, origin)
        End Function

        Public Overrides Sub SetLength(ByVal value As Long)
            OutputStream.SetLength(value)
        End Sub

        Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)
            Dim rawHtml As String = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count)

            Dim EndOfHtml As New Regex("</html>", RegexOptions.IgnoreCase)

            If Not EndOfHtml.IsMatch(rawHtml) Then
                ResponseHTML.Append(rawHtml)
            Else
                ResponseHTML.Append(rawHtml)

                Dim FilteredHtml As String = ResponseHTML.ToString
                CurrentMatchReplace = "xmlns:msgr=""http://messenger.live.com/2009/ui-tags"" >"

                'TODO:  Should add in filter optimization >> http://www.pcreview.co.uk/forums/thread-2359210.php
                FilteredHtml = Regex.Replace(FilteredHtml, _
                "(?<= \<html.*)\>", _
                AddressOf ApplyFilter, _
                RegexOptions.IgnoreCase Or _
                RegexOptions.CultureInvariant Or _
                RegexOptions.IgnorePatternWhitespace Or _
                RegexOptions.Compiled)

                Dim data As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(FilteredHtml)
                OutputStream.Write(data, 0, data.Length)
            End If

        End Sub

        Public Function ApplyFilter(ByVal OutputMatch As Match) As String
            'TODO:  Will want to apply some smarts here to handle group replacement
            Return CurrentMatchReplace
        End Function
#End Region

    End Class
End Namespace
